<template>
  <div class="mod-config">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="150px">
      <el-form-item label="视频地址" prop="videoUrl">
        <el-input type="textarea"  :rows="5" v-model="dataForm.videoUrl" placeholder="视频链接不能出现“&#《》<>”等特殊字符，否则将生成失败：每条视频链接后回车输入"></el-input>
      </el-form-item>
      <el-form-item label="选择站点" prop="siteId">
        <el-select v-model="dataForm.siteId" placeholder="请选择"  @change="handleChange(dataForm.siteId)">
          <el-option
            v-for="item in sites"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="不同短链接数量" prop="shortNum">
        <el-select v-model="dataForm.shortNum" placeholder="请选择" >
          <el-option
            v-for="item in shortNums"
            :key="item.label"
            :label="item.label"
            :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="推广链接" prop="shortUrls">
        <el-input type="textarea"  :rows="5" v-model="dataForm.shortUrls" placeholder="推广链接"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">保存视频链接</el-button>
      <el-button type="primary" @click="saveShort()">生成短链</el-button>
    </span>
  </div>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        wenzivisible: false,
        webOpenTypes: [{
          webOpenType: 0,
          label: '封禁中'
        }, {
          webOpenType: 1,
          label: '正常'
        }],
        shortNums: [{
          shortNum: 1,
          label: '1'
        }, {
          shortNum: 5,
          label: '5'
        }, {
          shortNum: 10,
          label: '10'
        }],
        sites: [],
        videoUrls: [],
        members: [],
        newData: [],
        dataForm: {
          id: 0,
          siteId: '',
          shortNum: 5,
          videoUrl: '',
          shortUrls: '',
          status: '1',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          siteId: [
            { required: true, message: '站点不能为空', trigger: 'blur' }
          ],
          videoUrl: [
            { required: true, message: '视频地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    activated () {
      this.init()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.dataForm.status = 1
        var params = {
        }
        API.basemember.select(params).then(({data}) => {
          this.members = data && data.code === 0 ? data.list : []
          if (this.members[0].videoType === 0) {
            this.wenzivisible = false
            this.dataForm.videoUrl = ''
          } else {
            this.wenzivisible = true
          }
        })
        API.baseadvvideo.select(params).then(({data}) => {
          this.videoUrls = data && data.code === 0 ? data.list : []
        })
        API.basesite.select(params).then(({data}) => {
          this.sites = data && data.code === 0 ? data.list : []
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.id) {
            API.basesiteadvvideo.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.basesiteadvvideo.id
                this.dataForm.siteId = data.basesiteadvvideo.siteId
                this.dataForm.videoUrl = data.basesiteadvvideo.videoUrl
                this.dataForm.status = data.basesiteadvvideo.status
                this.dataForm.createUserId = data.basesiteadvvideo.createUserId
                this.dataForm.createTime = data.basesiteadvvideo.createTime
              }
            })
          }
        })
      },
      handleChange: function (siteId) {
        console.log(siteId)
        API.basesiteadvvideo.infoSite(siteId).then(({data}) => {
          if (data && data.code === 0) {
            if (data.basesiteadvvideo) {
              this.dataForm.id = data.basesiteadvvideo.id
              this.dataForm.siteId = data.basesiteadvvideo.siteId
              this.dataForm.videoUrl = data.basesiteadvvideo.videoUrl
              this.dataForm.status = data.basesiteadvvideo.status
              this.dataForm.createUserId = data.basesiteadvvideo.createUserId
              this.dataForm.createTime = data.basesiteadvvideo.createTime
            } else {
              this.dataForm.videoUrl = ''
            }
          }
        })
      },
      saveShort () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.newData === undefined || this.newData.length === 0) {
              this.$message({
                message: '请先保存视频链接再进行生成短链',
                type: 'error',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                }
              })
              return false
            }
            var shortNumOb = {'shortNum': this.dataForm.shortNum}
            this.newData.push(shortNumOb)
            var tick = API.basesiteadvvideo.addShortRandom(this.newData)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shortUrls = data.shortUrls
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                  }
                })
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'siteId': this.dataForm.siteId,
              'videoUrl': this.dataForm.videoUrl,
              'status': this.dataForm.status,
              'createUserId': this.dataForm.createUserId,
              'createTime': this.dataForm.createTime
            }
            var tick = API.basesiteadvvideo.addRandom(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.newData = data.data
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
